<template>
  <section class="msg-list">
    <mark-down-dialog :visible.sync="dialogVisible"
                      title="内容详情" :mdContent="msg.content"></mark-down-dialog>
    <el-table :data="tableData" stripe height="calc(100vh - 215px)">
      <el-table-column prop="title" label="标题" width="200">
      </el-table-column>
      <el-table-column v-if="who==='all'" prop="uid" label="UserID" width="100">
      </el-table-column>
      <el-table-column v-if="who==='all'" label="可用" width="60">
        <template slot-scope="scope">
          <i v-if="scope.row.enable" class="el-icon-success"></i>
          <i v-else class="el-icon-error"></i>
        </template>
      </el-table-column>
      <el-table-column label="公开" width="60">
        <template slot-scope="scope">
          <i v-if="scope.row.public" class="el-icon-success"></i>
          <i v-else class="el-icon-error"></i>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.created_at|dateFormat('YYYY-MM-DD hh:mm:ss')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click.native.prevent="view(scope.row)"
                     type="text" size="small">
            正文<i class="el-icon-d-arrow-right"></i>
          </el-button>
          <el-button @click.native.prevent="toggle(scope.row)"
                     type="text" size="small">
            {{scope.row.enable?'禁用':'启用'}}<i class="el-icon-d-arrow-right"></i>
          </el-button>
          <el-button @click.native.prevent="del(scope.row)"
                     type="text" size="small">
            删除<i class="el-icon-d-arrow-right"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :style="{marginTop:'10px'}"
      @size-change="sizeChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 40, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      background
      @current-change="listPage"
      :total="total">
    </el-pagination>
  </section>
</template>

<script>
import {MsgService} from '@/service/api'
import MarkDownDialog from '@/components/common/MarkDownDialog'

export default {
  components: {
    'mark-down-dialog': MarkDownDialog
  },
  data () {
    return {
      total: 0,
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      msg: {
        content: ''
      },
      dialogVisible: false,
      who: this.$route.params.who
    }
  },
  mounted: function () {
    this.listPage(1)
  },
  watch: {
    '$route': 'listPage'
  },
  methods: {
    sizeChange: function (val) {
      this.pageSize = val
      this.listPage(1, val)
    },
    listPage: function (val) {
      this.who = this.$route.params.who
      switch (this.who) {
        case 'me':
          MsgService.pageMe(val, 5)
            .then(res => {
              if (res) {
                this.tableData = res.msgs
                this.total = res.total
              }
            })
          break
        case 'all':
          MsgService.pageAll(val, 5)
            .then(res => {
              if (res) {
                this.tableData = res.msgs
                this.total = res.total
              }
            })
          break
        default:
      }
    },
    view: function (item) {
      MsgService.one(item.id)
        .then(res => {
          this.msg = res.msg
          this.dialogVisible = true
        })
    },
    del: function (item) {
      // todo delete
    },
    toggle: function (item) {
      MsgService.toggle(item.id)
        .then(res => {
          if (res === 'success') {
            this.$message.success('修改成功')
            item.enable = !item.enable
          }
        })
    }
  }
}
</script>

<style scoped>
</style>
